package service;

import java.util.HashMap;
import java.util.Map;

public class Main {
    public static int solution(int n, int m, String s, String c) {
        // 统计货架上每种商品的数量
        Map<Character, Integer> countMap = new HashMap<>();
        for (char ch : s.toCharArray()) {
            countMap.put(ch, countMap.getOrDefault(ch, 0) + 1);
        }

        // 记录顾客想要购买的商品顺序
        int soldCount = 0;
        for (char ch : c.toCharArray()) {
            // 如果货架上有顾客想要的商品
            if (countMap.containsKey(ch) && countMap.get(ch) > 0) {
                // 卖出一件商品
                soldCount++;
                // 更新货架上该商品的数量
                countMap.put(ch, countMap.get(ch) - 1);
            }
        }

        return soldCount;
    }

    public static void main(String[] args) {
        System.out.println(solution(3, 4, "abc", "abcd") == 3);
        System.out.println(solution(4, 2, "abbc", "bb") == 2);
        System.out.println(solution(5, 4, "bcdea", "abcd") == 4);
    }
}